#include <simplemq/simplemq_redis.hpp>
#include <date_utilits.h>
#include "BaseCmdParam.h"
/************************************************************************/
/* REDIS client 连接测试                                                */
/************************************************************************/
using namespace simplemq;
using namespace gdface::mt;

// 命令行参数解析器
class ClientConfig : public gdface::BaseClientConfig {
public:
	const std::string PARAM_CHANNEL_NAME = "channel";
	const std::string PARAM_AUTH = "auth";
	/** 频道名，默认'c1' */
	std::string channel;
	std::string password;
	ClientConfig() :gdface::BaseClientConfig(), channel(), password() {
		cmdparam.add<std::string>(PARAM_CHANNEL_NAME, 'c', "channel name for subscribing", false, "c1");
		cmdparam.add<std::string>(PARAM_AUTH, 'a', "password for authorization", false, "");
	}
	~ClientConfig() = default;
	void afterParse() {
		gdface::BaseClientConfig::afterParse();
		channel = cmdparam.get<std::string>(PARAM_CHANNEL_NAME);
		password = cmdparam.get<std::string>(PARAM_AUTH);
	}
};

std::string now_str() {
	auto now = std::chrono::system_clock::now();
	auto mills = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
	return date::format_iso8601(mills);
}
ClientManager cmgr;
int
main(int argc, char *argv[]) {
	ClientConfig param;
	param.parse(argc, argv);
	auto rm = cmgr.make_client_manager(mq_client_params{ param.host, param.port, param.password });
	for (int i = 0; i < 100; ++i) {
		auto guard = rm->resource_guard();
		auto &client = (**guard);
		client.publish(param.channel, now_str().append(" ").append(std::to_string(i)));
		client.sync_commit();
	}
}
